package com.cpe.web.controller.student;

import static com.sql.data.domain.user.UserRespCode.INVALID_TOKEN;
import static com.sql.data.response.CommonRespCode.EXCEPTION;
import static com.sql.data.response.CommonRespCode.INVALID_REQUEST;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpe.data.domain.member.Member;
import com.cpe.data.domain.student.classproject.StudentClassProject;
import com.cpe.data.domain.student.classproject.StudentClassProjectForm;
import com.cpe.repository.mapper.member.MemberMapper;
import com.cpe.repository.mapper.student.StudentClassProjectMapper;
import com.cpe.transfer.student.StudentClassProjectTransfer;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sql.data.response.JsonResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * 学生课堂成绩
 */
@Slf4j
@RestController
@RequestMapping("/student_class_projects")
public class StudentClassProjectController {

	@Resource
	private StudentClassProjectMapper studentClassProjectMapper;

	@Resource
	private StudentClassProjectTransfer studentClassProjectTransfer;

	@Resource
	private MemberMapper memberMapper;

	/**
	 * 录入学生课堂成绩
	 */
	@Transactional
	@RequestMapping(method = POST)
	public JsonResponse add(@RequestBody StudentClassProjectForm form, HttpSession session,
			@RequestHeader String openid, ModelMap modelMap) {
		Member member = (Member) session.getAttribute("loginer");
		if (member == null) {
			// 尝试从Header里读取openid
			member = memberMapper.findByOpenid(openid);
			if (member == null) {
				return JsonResponse.custom(INVALID_TOKEN);
			}
			modelMap.addAttribute("loginer", member);
		}

		form.setCreator(member.getId());
		if (form.stopCreate()) {
			return JsonResponse.custom(INVALID_REQUEST);
		}

		String operate = "create";
		// 数据库校验
		StudentClassProject query = new StudentClassProject();
		query.setClassProjectId(form.getClassProjectId());
		query.setStudentId(form.getStudentId());
		if (studentClassProjectMapper.findOne(query) != null) {
			operate = "modify";
		}

		try {
			StudentClassProject studentClassProject = studentClassProjectTransfer.toEntity(form);
			switch (operate) {
			case "create": {
				studentClassProjectMapper.insert(studentClassProject);
				break;
			}
			case "modify": {
				studentClassProjectMapper.updateById(studentClassProject);
				break;
			}
			default:
				break;
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return JsonResponse.custom(EXCEPTION);
		}
		return JsonResponse.ok();
	}

	@RequestMapping(value = "/by_parent/{parentId}", method = GET)
	public JsonResponse getStudentClassProjectByParent(@PathVariable String parentId) {
		List<StudentClassProject> list = studentClassProjectMapper.findByParentId(parentId);
		// 根据学生ID划分list
		Map<String, List<StudentClassProject>> map = Maps.newHashMap();
		for (StudentClassProject info : list) {
			if (map.containsKey(info.getStudentId())) {
				map.get(info.getStudentId()).add(info);
			} else {
				map.put(info.getStudentId(), Lists.newArrayList(info));
			}
		}

		List<List<StudentClassProject>> listGroup = Lists.newArrayList();
		for (Entry<String, List<StudentClassProject>> entry : map.entrySet()) {
			listGroup.add(entry.getValue());
		}

		return JsonResponse.ok(studentClassProjectTransfer.toInfoListGroup(listGroup));
	}
}
